package com.example.upload.filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.filter.CorsFilter;


/**
 * Content :过滤器配置中心
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean initCORSFilter(){
        CorsFilter corsFilter=new CorsFilter(httpServletRequest-> {
                CorsConfiguration config = new CorsConfiguration();
                config.setAllowCredentials(true);
                config.addAllowedOrigin("*");
                config.addAllowedHeader("*");
                config.addAllowedMethod("*");
                return config;
        });
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(corsFilter);
        registration.addUrlPatterns("/*");
        registration.addInitParameter("exclusions","*.js,*.gif,*.json,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        registration.setOrder(1);
        return registration;
    }
}